﻿/*
   Author: Tomer Paz
   games.com
   2007
*/
//the Field Viewer (Optional Abstract class, for future enhanced implementations)
package monopoly.views {
	import flash.display.Sprite;
	import fl.controls.TextArea;
	import fl.containers.ScrollPane;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import monopoly.views.BottomPanel;
	import flash.display.BlendMode;
	import flash.events.MouseEvent;
	import monopoly.Field.Field;
	import flash.events.Event;
	// add filters for nicer view
	import flash.filters.DropShadowFilter;
	import flash.filters.BevelFilter;
	import flash.filters.GlowFilter;

	
	public class FieldView extends Sprite
		{
		protected var _data:Field;
		protected var _Position:uint;
		// add filters for nicer view
		protected var _filtersArray:Array;
		protected var _StatusBox:Sprite;
		protected var _tArea:TextArea;
		protected var _rect:Shape;
		protected var _loader:Loader;
		protected var FirstTimeFlag:Boolean = true;

		public function FieldView(Data:Field)
			{
			/* I am a Field View -> attached to my personal Field data */
			
			_data = Data;
			
			SetupStatusBox();
			
			/* add graphic filters */
			
			applyFilters();
			
			/* whenever field data is updated I shall be informed and redraw */
			
			_data.addEventListener(Event.CHANGE, draw);

			// register mouse-over events
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			}

		protected function applyFilters():void
			{
			// Create a DropShadowFilter instance.
			var dropShadow:DropShadowFilter = new DropShadowFilter();
			var bevel:BevelFilter = new BevelFilter();
			var glow:GlowFilter = new GlowFilter();
			
			// allocate the filters array, adding the filters to the array by passing it as 
			// parameters to the Array() constructor.
			_filtersArray = new Array(bevel, glow, dropShadow);
			// Assign the filters array to the display object to apply the filter.
			this.filters = _filtersArray;
			}
		
		/* draw the field */
		
		protected function draw(event:Event):void
			{
			_Position = _data.Position;
			//graphics.lineStyle(0, 0x000000, 1, false, "normal");
			if (null == _rect) 
				{
				_rect = new Shape();
				_rect.graphics.beginFill(0xFFFFFF); 	//_PlayersColor[_data.Owner.ID]);
				_rect.graphics.drawRect(_data.X, _data.Y, _data.Size, _data.Size);
				_rect.graphics.endFill( );
				addChild(_rect);
				}
			
			
			_loader = _data.ActiveProperty;

			_loader.content.mask = _rect;		// this;
			_loader.content.x = _data.X;
			_loader.content.y = _data.Y;
			
			_loader.content.blendMode = BlendMode.LAYER;
			if (FirstTimeFlag)
				{
				addChildAt(_loader.content, 0);
				FirstTimeFlag = false;
				}
			this.blendMode	 = BlendMode.LAYER;
			}
		
		protected function mouseOverHandler(event:MouseEvent):void
			{
        	if (event.currentTarget == this)
				{
				_loader.content.scaleX = 1.2;
				_loader.content.scaleY = 1.2;
				draw_statusBox();
				}
			}
		protected function mouseOutHandler(event:MouseEvent):void
			{
			if (event.currentTarget == this)
				{
				_loader.content.scaleX = 1.0;
				_loader.content.scaleY = 1.0;
				undraw_statusBox();
				}
    		}
			
		protected function SetupStatusBox():void
			{
			_StatusBox  = new Sprite();
			_tArea		= new TextArea();
			
			_tArea.editable = false;
			_tArea.name	= _data.PropertyName;
			_tArea.setSize(100,100);
			_tArea.alpha = 1;
			_tArea.x = 0;
			_tArea.y = 0;
			
			_tArea.visible	   = false;
			_StatusBox.visible = false;
			
			_StatusBox.name = BottomPanel.BPANEL_CENTER;
			_StatusBox.addChild(_tArea);
			_StatusBox.x  = 20;
			_StatusBox.y = 20;
			}
		public function attachStatusToPanel():Sprite
			{
			return _StatusBox;
			}
		
		protected function draw_statusBox():void
			{
			_tArea.text = _tArea.name + "\n";
			_tArea.appendText("Owner: " + _data.Owner.Name + "\n");
			_tArea.appendText("Cost: " + _data.Cost + "\n");
			_tArea.appendText("Group: " + _data.Group + "\n");
			_tArea.appendText("Fee: " + _data.Fee + "\n");
			_tArea.appendText("Status: " + _data.Status);
			_tArea.alpha = 1;
			
			_StatusBox.alpha	= 1;
			_tArea.visible		= true;
			_StatusBox.visible  = true;
			}
			
		protected function undraw_statusBox():void
			{
			_tArea.visible		= false;
			_StatusBox.visible  = false;
			}
		}
}